#include "StdAfx.h"
#include "ServerMsg.h"

#include <iostream>
#include "ChatServer.h"
#include "Message.h"
#include <sstream>


using namespace std;

ClinetMsg::ClinetMsg(void)
{
}

ClinetMsg::~ClinetMsg(void)
{
}

void ClinetMsg::Do()
{
	 cout <<"nothing"<<endl;
}

void ServerCmd::Do()
{
	
	stringstream buff;
	buff<<msg;
	size_t sz = buff.str().size() + 1;

	for(std::list<ClientObj*>::iterator it = ChatServer::Clients.begin(); it != ChatServer::Clients.end();++it)
	{
		ClientObj* aclient = *it;
		CT_MSG_S2C * msg = new CT_MSG_S2C;

		strcpy_s(msg->str,sz,buff.str().c_str());
		msg->len = sz;
		aclient->msgqueue.push(msg);
	}

}

void UserLogin::Do()
{
	
	cout <<"User #"<<m_Client->id<<" login."<<endl;
	

	 stringstream buff;
	 buff<<"Client #"<<m_Client->id<<" join.";
	 size_t sz = buff.str().size() + 1;

	 for(std::list<ClientObj*>::iterator it = ChatServer::Clients.begin(); it != ChatServer::Clients.end();++it)
	 {
		 ClientObj* aclient = *it;
		 CT_MSG_S2C * msg = new CT_MSG_S2C;
		 
		 strcpy_s(msg->str,sz,buff.str().c_str());
		  msg->len = sz;
		 aclient->msgqueue.push(msg);
	 }

}

void Userquit::Do()
{
	cout <<"User #"<<m_Client->id<<" quit."<<endl;

	stringstream buff;
	buff<<"Client #"<<m_Client->id<<" quit.";
	size_t sz = buff.str().size() + 1;

	ChatServer::Clients.remove(m_Client);

	for(std::list<ClientObj*>::iterator it = ChatServer::Clients.begin(); it != ChatServer::Clients.end();++it)
	{
		ClientObj* aclient = *it;
		CT_MSG_S2C * msg = new CT_MSG_S2C;

		strcpy_s(msg->str,sz,buff.str().c_str());
		msg->len = sz;
		aclient->msgqueue.push(msg);
	}

	delete m_Client;
}
